<?php
namespace User\Controller;

use Common\Controller\BehaviorController;
use Common\Model\InfoContentModel;
class InfoController extends BehaviorController
{
    public function index()
    {

    }

    public function minfo()
    {
        $id = I("id");
        if ($id) {
            $info = new InfoContentModel();
            $data = $info->find($id);
            if ($data) {
                $this->assign('data', $data);
                $this->display("/user/minfo");
                exit();
            }
        }
        $this->redirect('/info/info/error404', array('id' => $id));
    }

    public function refreshdate()
    {
        $pass = I('post.pass');
        $id = I('post.id');
        if (!empty($id) && !empty($pass)) {
            $infoContentModel = M("info_content");
            $info = $infoContentModel->where("id={$id}")->find();
            if (!empty($info)) {
                if (md5($info['password']) == md5($pass)) {
                    $data = array(
                        'id' => $id,
                        'valid_date' => date("Y-m-d H:i:s", strtotime("+30 days")),
                        'create_time' => date('Y-m-d H:i:s', time())
                    );
                    $infoContentModel->save($data);
                    $this->success("更新成功");
                    exit();
                } else {
                    $this->error("管理密码不正确");
                    exit();
                }
            }
        }
        $this->error("非法请求", '/');
    }

    public function deleteInfo()
    {
        $pass = I('post.pass');
        $id = I('post.id');
        if (!empty($id) && !empty($pass)) {
            $infoContentModel = M("info_content");
            $info = $infoContentModel->where("id={$id}")->find();
            if (!empty($info)) {
                if (md5($info['password']) == md5($pass)) {
                    $data = array(
                        'id' => $id,
                        'valid_date' => date("Y-m-d H:i:s", strtotime("+30 days")),
                        'create_time' => date('Y-m-d H:i:s', time())
                    );
                    $infoContentModel->delete($info['id']);
                    $this->success("删除成功");
                    exit();
                } else {
                    $this->error("管理密码不正确");
                    exit();
                }
            }
        }
        $this->error("非法请求", '/');
    }

    public function editInfo()
    {
        $pass = empty(I('post.pass')) ? I('get.pass') : I('post.pass');
        $id = empty(I('post.id')) ? I('get.id') : I('post.id');

        if(!empty($id) && !empty($pass)){
            $infoModel = new InfoContentModel();
            $infoData = $infoModel->getInfoById($id);
            if(md5($infoData['password']."58shiyan") == $pass || $infoData['password'] == $pass) {
                if (IS_POST && I('get.type') == 1) {
                    //$infoModel = new InfoContentModel();
                    $result = $infoModel->editeInfo($id);
                    if ($result) {
                        $this->success('编辑成功', "/xinxi-{$id}.html");
                    } else {
                        $this->error($infoModel->getError());
                    }
                    exit();
                }
                $area = C("city_area");
                $this->assign("info", $infoData)->assign("area", $area);
                $this->display(':post_edit');
                exit();
            }
        }

        $this->error("请重新输入密码");

    }
}